package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.Dao.Impl.CompanyDaoImpl;
import com.ysd.Dao.Impl.DesignDaoImpl;
import com.ysd.entity.Company;
import com.ysd.entity.Design;
import com.ysd.util.BaseServlet;
import com.ysd.util.DBmanager;
import com.ysd.util.Result;

public class AdminCompanyServlet extends BaseServlet {
	/**
	 * 查询公司全部信息
	 * http://localhost:8080/zhubao/adminCompany?reqName=getCompany
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getCompany(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		CompanyDaoImpl cDao = new CompanyDaoImpl();//创建公司实现类
		Company company = cDao.getCompany();//调用公司全查方法

		response.setCharacterEncoding("UTF-8");//设置响应编码格式
		PrintWriter out = response.getWriter();
		
		Gson gson= new Gson();
		String res = gson.toJson(company);//将company转化为json字符串
		out.print(res);//响应数据
		System.out.println(res);
		
		out.flush();
		out.close();
	
	}
	/**
	 * 编辑公司信息
	 * localhost:8080/zhubao/adminCompany?reqName=editCompany
	 * @author 86176
	 *
	 */
	public void editCompany(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		CompanyDaoImpl cDao = new CompanyDaoImpl();//创建公司实现类
		
		String name =request.getParameter("name");
		String webSite =request.getParameter("webSite");
		String introduction =request.getParameter("introduction");
		String phone =request.getParameter("phone");
		String email =request.getParameter("email");
		String address =request.getParameter("address");
		String cultrue =request.getParameter("cultrue");
		String introducePicture =request.getParameter("picName");
		
		if(name != null && !name.equals("")&& webSite!= null && !webSite.equals("")&& introduction!= null && !introduction.equals("")
				&& phone != null && !phone.equals("")&& email != null && !email.equals("") &&  address != null && !address.equals("")
				&& cultrue!= null && !cultrue.equals("")&& introducePicture !=null && !introducePicture.equals("") ){
			Company cp = new Company(name, webSite, introduction, phone, email, address, cultrue, introducePicture, null, null, null);
			int n =cDao.editCompany(cp);//调用编辑设计师方法
			
			if (n>0) {//修改成功
				out.print(Result.toClient(true, "修改成功",""));//{"success":true,"message":"修改成功","remark":""}	
			} else {//修改失败
				out.print(Result.toClient(false, "修改失败",""));//{"success":false,"message":"修改失败","remark":""}	
			}
			
	}else {
		out.print(Result.toClient(false, "修改失败,字段为空",""));//图片路径或设计师名或设计师级别为空
	}
	
	out.flush();
	out.close();
	}

}
